package com.agency8x.controller;

import com.agency8x.bean.ResultInfoBean;
import com.agency8x.bean.SearchParamBean;
import com.agency8x.interceptor.CheckLoginInterceptor;
import com.agency8x.model.BankcardModel;
import com.agency8x.model.MiddlemanModel;
import com.agency8x.model.RechargeModel;
import com.agency8x.model.TransactionDetailModel;
import com.agency8x.model.WithdrawModel;
import com.agency8x.web.util.ControllerUtil;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.token.TokenManager;

/**
 * 居间商页面控制器
 *
 * @author SoChishun
 *
 */
@Before(CheckLoginInterceptor.class)
public class MiddlemanController extends Controller {

	// 获取和设置查询参数
	private SearchParamBean getAndSetQueryParams(){
		SearchParamBean bean=new SearchParamBean();
		bean.setWord(getPara("word", ""));
		bean.setDate1(getPara("date1", ""));
		bean.setDate2(getPara("date2", ""));
		this.setAttr("word", bean.getWord());
		this.setAttr("date1", bean.getDate1());
		this.setAttr("date2", bean.getDate2());
		return bean;
	}
	
    public void index() {
        String word = this.getPara("word", "");
        Page<MiddlemanModel> list = MiddlemanModel.me.paginate(getParaToInt(0, 1), 10, word);
        setAttr("listPage", list);
        // 设置传入分页的url参数
        this.setAttr("append", "?word=" + word);
        this.render("index.html");
    }

    /**
     * 返回MiddlemanModel
     *
     * @param top_agent_id
     * @return
     */
    private MiddlemanModel setAgentInfoData(Integer top_agent_id) {
        MiddlemanModel data = MiddlemanModel.me.getRecord(top_agent_id);
        setAttr("agentinfoData", data);
        return data;
    }

    /**
     * 下线代理
     */
    public void childagent() {
    	SearchParamBean search=getAndSetQueryParams();
        String tplfile = new Throwable().getStackTrace()[1].getMethodName() + ".html";
        // 获取请求参数
        String user_type = this.getPara("user_type", ""); // 用户类型,c=下线,c1=1级下线,c2=2级下线
        Integer top_agent_id = this.getParaToInt("top_agent_id", 0); // 顶级代理ID
        // 页面模板赋值
        this.setAttr("user_type", user_type);
        this.setAttr("top_agent_id", top_agent_id);
        this.setAttr("tplfile", tplfile);
        setAgentInfoData(top_agent_id); // 设置表格头部用户属性表
        setAttr("listPage", MiddlemanModel.me.getAllChildAgentPaginate(getParaToInt(0, 1), 10, top_agent_id, search.getWord(),search.getDate1(),search.getDate2()));
        // 设置传入分页的url参数
        this.setAttr("append", search.getQueryString() + "&user_type=" + user_type + "&top_agent_id=" + top_agent_id);
        this.render("childagent.html"); // childagent
    }

    /**
     * 下线账户明细/日结账单
     */
    public void transaction_detail() {
    	SearchParamBean search=getAndSetQueryParams();
        // 获取请求参数
        String user_type = this.getPara("user_type", ""); // 用户类型,c=下线,c1=1级下线,c2=2级下线
        Integer top_agent_id = this.getParaToInt("top_agent_id", 0); // 顶级代理ID
        // 页面模板赋值
        this.setAttr("user_type", user_type);
        this.setAttr("top_agent_id", top_agent_id);
        MiddlemanModel infoModel = setAgentInfoData(top_agent_id); // 设置表格头部用户属性表
        if (user_type.equals("c")) {
            setAttr("listPage", TransactionDetailModel.me.getPaginateByTopAgentId(getParaToInt(0, 1), 10, top_agent_id, search.getWord(),search.getDate1(),search.getDate2()));
        } else {
            setAttr("listPage", TransactionDetailModel.me.getPaginateByUserId(getParaToInt(0, 1), 10, infoModel.getUserId(), search.getWord(),search.getDate1(),search.getDate2()));
        }
        // 设置传入分页的url参数
        this.setAttr("append", search.getQueryString() + "&user_type=" + user_type + "&top_agent_id=" + top_agent_id);
        this.render("transaction_detail.html");
    }

    /**
     * 下线提现明细/提现记录
     */
    public void withdraw_pending_list() {
    	SearchParamBean search=getAndSetQueryParams();
        // 获取请求参数
        String user_type = this.getPara("user_type", ""); // 用户类型,c=下线,c1=1级下线,c2=2级下线
        Integer top_agent_id = this.getParaToInt("top_agent_id", 0); // 顶级代理ID
        // 页面模板赋值
        this.setAttr("user_type", user_type);
        this.setAttr("top_agent_id", top_agent_id);
        MiddlemanModel infoModel = setAgentInfoData(top_agent_id); // 设置表格头部用户属性表
        if (user_type.equals("c")) {
            setAttr("listPage", WithdrawModel.me.getPaginateByTopAgentId(getParaToInt(0, 1), 10, top_agent_id, 0, search.getWord(),search.getDate1(),search.getDate2()));
        } else {
            setAttr("listPage", WithdrawModel.me.getPaginateByUserId(getParaToInt(0, 1), 10, infoModel.getUserId(), 0, search.getWord(),search.getDate1(),search.getDate2()));
        }
        // 设置传入分页的url参数
        this.setAttr("append", search.getQueryString() + "&user_type=" + user_type + "&top_agent_id=" + top_agent_id);
        this.render("withdraw_pending_list.html");
    }

    public void withdraw_success_list() {
    	SearchParamBean search=getAndSetQueryParams();
        // 获取请求参数
        String user_type = this.getPara("user_type", ""); // 用户类型,c=下线,c1=1级下线,c2=2级下线
        Integer top_agent_id = this.getParaToInt("top_agent_id", 0); // 顶级代理ID
        // 页面模板赋值
        this.setAttr("user_type", user_type);
        this.setAttr("top_agent_id", top_agent_id);
        MiddlemanModel infoModel = setAgentInfoData(top_agent_id); // 设置表格头部用户属性表
        if (user_type.equals("c")) {
            setAttr("listPage", WithdrawModel.me.getPaginateByTopAgentId(getParaToInt(0, 1), 10, top_agent_id, 1, search.getWord(),search.getDate1(),search.getDate2()));
        } else {
            setAttr("listPage", WithdrawModel.me.getPaginateByUserId(getParaToInt(0, 1), 10, infoModel.getUserId(), 1, search.getWord(),search.getDate1(),search.getDate2()));
        }
        // 设置传入分页的url参数
        this.setAttr("append", search.getQueryString() + "&user_type=" + user_type + "&top_agent_id=" + top_agent_id);
        this.render("withdraw_success_list.html");
    }

    public void withdraw_rejected_list() {
    	SearchParamBean search=getAndSetQueryParams();
        // 获取请求参数
        String user_type = this.getPara("user_type", ""); // 用户类型,c=下线,c1=1级下线,c2=2级下线
        Integer top_agent_id = this.getParaToInt("top_agent_id", 0); // 顶级代理ID
        // 页面模板赋值
        this.setAttr("user_type", user_type);
        this.setAttr("top_agent_id", top_agent_id);
        MiddlemanModel infoModel = setAgentInfoData(top_agent_id); // 设置表格头部用户属性表
        if (user_type.equals("c")) {
            setAttr("listPage", WithdrawModel.me.getPaginateByTopAgentId(getParaToInt(0, 1), 10, top_agent_id, 2, search.getWord(),search.getDate1(),search.getDate2()));
        } else {
            setAttr("listPage", WithdrawModel.me.getPaginateByUserId(getParaToInt(0, 1), 10, infoModel.getUserId(), 2, search.getWord(),search.getDate1(),search.getDate2()));
        }
        // 设置传入分页的url参数
        this.setAttr("append", search.getQueryString() + "&user_type=" + user_type + "&top_agent_id=" + top_agent_id);
        this.render("withdraw_rejected_list.html");
    }

    /**
     * 下线充值明细
     */
    public void recharge_online_list() {
    	SearchParamBean search=getAndSetQueryParams();
        // 获取请求参数
        String user_type = this.getPara("user_type", ""); // 用户类型,c=下线,c1=1级下线,c2=2级下线
        Integer top_agent_id = this.getParaToInt("top_agent_id", 0); // 顶级代理ID
        // 页面模板赋值
        this.setAttr("user_type", user_type);
        this.setAttr("top_agent_id", top_agent_id);
        setAgentInfoData(top_agent_id); // 设置表格头部用户属性表
        setAttr("listPage", RechargeModel.me.getPaginateByTopAgentId(getParaToInt(0, 1), 10, top_agent_id, 1, search.getWord(),search.getDate1(),search.getDate2()));
        // 设置传入分页的url参数
        this.setAttr("append", search.getQueryString() + "&user_type=" + user_type + "&top_agent_id=" + top_agent_id);
        this.render("recharge_online_list.html");
    }

    /**
     * 下线充值明细
     */
    public void recharge_direct_list() {
    	SearchParamBean search=getAndSetQueryParams();
        // 获取请求参数
        String user_type = this.getPara("user_type", ""); // 用户类型,c=下线,c1=1级下线,c2=2级下线
        Integer top_agent_id = this.getParaToInt("top_agent_id", 0); // 顶级代理ID
        // 页面模板赋值
        this.setAttr("user_type", user_type);
        this.setAttr("top_agent_id", top_agent_id);
        setAgentInfoData(top_agent_id); // 设置表格头部用户属性表
        setAttr("listPage", RechargeModel.me.getPaginateByTopAgentId(getParaToInt(0, 1), 10, top_agent_id, 1, search.getWord(),search.getDate1(),search.getDate2()));
        // 设置传入分页的url参数
        this.setAttr("append", search.getQueryString() + "&user_type=" + user_type + "&top_agent_id=" + top_agent_id);
        this.render("recharge_direct_list.html");
    }

    /**
     * 下线充值明细
     */
    public void recharge_pending_list() {
    	SearchParamBean search=getAndSetQueryParams();
        // 获取请求参数
        String user_type = this.getPara("user_type", ""); // 用户类型,c=下线,c1=1级下线,c2=2级下线
        Integer top_agent_id = this.getParaToInt("top_agent_id", 0); // 顶级代理ID
        // 页面模板赋值
        this.setAttr("user_type", user_type);
        this.setAttr("top_agent_id", top_agent_id);
        setAgentInfoData(top_agent_id); // 设置表格头部用户属性表
        setAttr("listPage", RechargeModel.me.getPaginateByTopAgentId(getParaToInt(0, 1), 10, top_agent_id, 1, search.getWord(),search.getDate1(),search.getDate2()));
        // 设置传入分页的url参数
        this.setAttr("append", search.getQueryString() + "&user_type=" + user_type + "&top_agent_id=" + top_agent_id);
        this.render("recharge_pending_list.html");
    }

    /**
     * 下线充值明细
     */
    public void recharge_rejected_list() {
    	SearchParamBean search=getAndSetQueryParams();
        // 获取请求参数
        String user_type = this.getPara("user_type", ""); // 用户类型,c=下线,c1=1级下线,c2=2级下线
        Integer top_agent_id = this.getParaToInt("top_agent_id", 0); // 顶级代理ID
        // 页面模板赋值
        this.setAttr("user_type", user_type);
        this.setAttr("top_agent_id", top_agent_id);
        setAgentInfoData(top_agent_id); // 设置表格头部用户属性表
        setAttr("listPage", RechargeModel.me.getPaginateByTopAgentId(getParaToInt(0, 1), 10, top_agent_id, 1, search.getWord(),search.getDate1(),search.getDate2()));
        // 设置传入分页的url参数
        this.setAttr("append", search.getQueryString() + "&user_type=" + user_type + "&top_agent_id=" + top_agent_id);
        this.render("recharge_rejected_list.html");
    }

    /**
     * 下线充值明细
     */
    public void recharge_success_list() {
    	SearchParamBean search=getAndSetQueryParams();
        // 获取请求参数
        String user_type = this.getPara("user_type", ""); // 用户类型,c=下线,c1=1级下线,c2=2级下线
        Integer top_agent_id = this.getParaToInt("top_agent_id", 0); // 顶级代理ID
        // 页面模板赋值
        this.setAttr("user_type", user_type);
        this.setAttr("top_agent_id", top_agent_id);
        setAgentInfoData(top_agent_id); // 设置表格头部用户属性表
        setAttr("listPage", RechargeModel.me.getPaginateByTopAgentId(getParaToInt(0, 1), 10, top_agent_id, 1, search.getWord(),search.getDate1(),search.getDate2()));
        // 设置传入分页的url参数
        this.setAttr("append", search.getQueryString() + "&user_type=" + user_type + "&top_agent_id=" + top_agent_id);
        this.render("recharge_success_list.html");
    }

    /**
     * 银行卡
     */
    public void bankcard() {
        String user_type = this.getPara("user_type", ""); // 用户类型,c=下线,c1=1级下线,c2=2级下线
        Integer top_agent_id = this.getParaToInt("top_agent_id", 0);
        this.setAttr("user_type", user_type);
        this.setAttr("top_agent_id", top_agent_id);
        MiddlemanModel usermodel = setAgentInfoData(top_agent_id); // 设置表格头部用户属性表
        BankcardModel bankcardModel = BankcardModel.me.findByUserId(usermodel.getUserId());
        this.setAttr("bankcardModel", bankcardModel);
        this.render("bankcard.html");
    }

    /**
     * 编辑居间代理
     */
    public void edit() {
        String user_id = getPara("user_id");
        if (user_id != null && !"".equals(user_id.trim())) {
        	MiddlemanModel data=MiddlemanModel.me.MiddlemanEdit(user_id.toString());
            setAttr("itemData", data);
        }
        TokenManager.createToken(this, "token", 30);
        this.render("edit.html");
    }

    /**
     * 保存居间代理
     */
    public void save() {
        if (!TokenManager.validateToken(this, "token")) {
            ControllerUtil.renderDisptchJumpError(this, "操作超时或重复提交");
            return;
        }
        Integer user_id = getParaToInt("user_id");
        Integer sex = this.getParaToInt("sex");
        if (null == user_id) {
            Integer topAgentId = this.getParaToInt("top_agent_id");
            ResultInfoBean resultBean = MiddlemanModel.me.createMiddleman(topAgentId);
            if (resultBean.getResult()) {
                user_id = (Integer) resultBean.getObjectParam();
            }
        }
        boolean result = false;
        if (null != user_id) {
            result = MiddlemanModel.me.updateMiddleman(user_id, this.getPara("nickname", ""), this.getPara("email", ""),
                    this.getPara("mobile", ""), this.getPara("username", ""), this.getPara("password", ""), sex,this.getParaToInt("is_free_regist_agent"));
        }
        if (result) {
            ControllerUtil.renderDisptchJumpSuccess(this); // 操作成功，倒计时3秒后自动刷新列表页面
        } else {
            ControllerUtil.renderDisptchJumpError(this, "保存失败"); // 操作失败，倒计时3秒后，自动后退到提交的表单页面
        }
    }
    
    public String getSexText(Integer sex) {
    	switch(sex) {
    	case 1:
    		return "男";
    	case 2:
    		return "女";    	
    	}
    	return "-";
	}
}
